package com.lvy.hczn.front.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lvy.hczn.front.common.core.text.Convert;
import com.lvy.hczn.front.common.exception.UtilException;
import com.lvy.hczn.front.system.domain.business.Spu;
import com.lvy.hczn.front.system.mapper.SpuMapper;
import com.lvy.hczn.front.system.service.SpuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class SpuServiceImpl extends ServiceImpl<SpuMapper, Spu> implements SpuService {

    @Autowired
    private SpuMapper spuMapper;

    /**
     * 修改或保存
     *
     * @param spuInfo
     */
    @Override
    public Spu saveOrUpdateSpu(Spu spuInfo) {
        Spu spu = super.getOne(Wrappers.<Spu>lambdaQuery().eq(Spu::getSpuCode, spuInfo.getSpuCode()).eq(Spu::getSourceId, spuInfo.getSourceId()));
        if (spu != null) {
            spuInfo.setId(spu.getId());
            super.updateById(spuInfo);
        } else {
            super.save(spuInfo);
        }
        return spuInfo;
    }

    /**
     * 根据ID和温区查询
     *
     * @param spu
     * @return com.lvy.hczn.front.system.domain.business.Spu
     * @author yw
     * @date 2023-10-05 14:42:52
     */
    @Override
    public Spu selectByIdAndTemperatureCode(Spu spu) {
        if (StrUtil.isEmpty(spu.getId())) {
            throw new UtilException("ID不能为空");
        }
        if (spu.getTemperatureZoneCode() == null) {
            throw new UtilException("温区不能为空");
        }
        Spu model = super.getOne(Wrappers.<Spu>lambdaQuery().eq(Spu::getId, spu.getId()).eq(Spu::getTemperatureZoneCode, spu.getTemperatureZoneCode()));
        if (model == null) {
            throw new UtilException("货箱温区不能存放该商品,请检查");
        }
        return model;
    }

    /**
     * 查询原材料
     *
     * @param id 原材料主键
     * @return 原材料
     */
    @Override
    public Spu selectSpuById(String id) {
        return spuMapper.selectSpuById(id);
    }

    /**
     * 查询原材料列表
     *
     * @param spu 原材料
     * @return 原材料
     */
    @Override
    public List<Spu> selectSpuList(Spu spu) {
        return spuMapper.selectSpuList(spu);
    }

    /**
     * 新增原材料
     *
     * @param spu 原材料
     * @return 结果
     */
    @Override
    public int insertSpu(Spu spu) {
        return spuMapper.insertSpu(spu);
    }

    /**
     * 修改原材料
     *
     * @param spu 原材料
     * @return 结果
     */
    @Override
    public int updateSpu(Spu spu) {
        return spuMapper.updateById(spu);
    }

    /**
     * 批量删除原材料
     *
     * @param ids 需要删除的原材料主键
     * @return 结果
     */
    @Override
    public int deleteSpuByIds(String ids) {
        return spuMapper.deleteSpuByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除原材料信息
     *
     * @param id 原材料主键
     * @return 结果
     */
    @Override
    public int deleteSpuById(String id) {
        return spuMapper.deleteSpuById(id);
    }
}